package qeug.shsh.facde.controller;

import com.drew.lang.annotations.NotNull;
import com.huawei.shade.org.apache.commons.logging.Log;
import com.huawei.shade.org.apache.commons.logging.LogFactory;
import kd.bos.dataentity.OperateOption;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.operate.result.OperationResult;
import kd.bos.openapi.common.custom.annotation.ApiController;
import kd.bos.openapi.common.custom.annotation.ApiPostMapping;
import kd.bos.openapi.common.custom.annotation.ApiRequestBody;
import kd.bos.openapi.common.custom.annotation.ApiResponseBody;
import kd.bos.openapi.common.result.CustomApiResult;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.OperationServiceHelper;
import qeug.shsh.facde.info.BPMApplyOutputModel;
import qeug.shsh.facde.resultext.BPMApplyOutputResultExt;

import javax.validation.Valid;
import java.util.Date;

@ApiController(value = "BPMApplyOutputController", desc = "金蝶单据操作回传接口，自定义插件")

public class BPMApplyOutputController {

    private final static Log logger = LogFactory.getLog(BPMApplyOutputController.class);//日志

    @ApiPostMapping(value = "/shsh_bpmoutput", desc = "通用申请回传api接口")
    public CustomApiResult<@ApiResponseBody("返回参数") BPMApplyOutputResultExt> BPMApplyOutput
            (@NotNull @Valid @ApiRequestBody(value = "入参") BPMApplyOutputModel model) {

        BPMApplyOutputResultExt applyOutputResultExt = new BPMApplyOutputResultExt();

        String requsetid = model.getRequsetid();//招待流程id
        String soureSystem = model.getSoureSystem();//来源系统
        String billname = model.getBillname();//单据名称
        String operation = model.getOperation();//操作类型
        Date date = model.getDate();//操作日期

        QFilter[] f1 = new QFilter[]{
                new QFilter("qeug_processinstance", QCP.equals, requsetid)};
        DynamicObject dailyApplyBill = BusinessDataServiceHelper.loadSingle("er_dailyapplybill", f1);//事前申请单

//        dailyApplyBill.set("billstatus", operation);//单据状态默认为提交
        if (operation.equals("驳回")) {
            OperationResult bd_customer = OperationServiceHelper.executeOperate("unsubmit", "er_dailyapplybill", new DynamicObject[]{dailyApplyBill}, OperateOption.create());
        }else if (operation.equals("作废")){
            OperationResult bd_customer = OperationServiceHelper.executeOperate("invalid", "er_dailyapplybill", new DynamicObject[]{dailyApplyBill}, OperateOption.create());
        }else if (operation.equals("删除")){
            OperationResult bd_customer = OperationServiceHelper.executeOperate("delete", "er_dailyapplybill", new DynamicObject[]{dailyApplyBill}, OperateOption.create());
        }else if (operation.equals("审核")){
            OperationResult bd_customer = OperationServiceHelper.executeOperate("audit", "er_dailyapplybill", new DynamicObject[]{dailyApplyBill}, OperateOption.create());
        }else if (operation.equals("反审核")){
            OperationResult bd_customer = OperationServiceHelper.executeOperate("unaudit", "er_dailyapplybill", new DynamicObject[]{dailyApplyBill}, OperateOption.create());
        }
        return CustomApiResult.success(applyOutputResultExt);
    }
}
